package com.xinqi.modules.prepare.courseware.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.courseware.convert.PrepareCoursewarePartConvert;
import com.xinqi.modules.prepare.courseware.domain.PrepareCoursewareEntity;
import com.xinqi.modules.prepare.courseware.domain.PrepareCoursewarePartEntity;
import com.xinqi.modules.prepare.courseware.dto.req.PrepareCoursewarePartQueryDTO;
import com.xinqi.modules.prepare.courseware.dto.rsp.PrepareCoursewarePartResultDTO;
import com.xinqi.modules.prepare.courseware.manager.PrepareCoursewarePartManager;
import com.xinqi.modules.prepare.courseware.mapper.PrepareCoursewarePartMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 备课区互动课件切片管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareCoursewarePartManagerImpl extends BaseServiceImpl<PrepareCoursewarePartMapper, PrepareCoursewarePartEntity> implements PrepareCoursewarePartManager {
    private final PrepareCoursewarePartConvert prepareCoursewarePartConvert;

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareCoursewarePartResultDTO> findPage(Page<Object> page, PrepareCoursewarePartQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareCoursewarePartResultDTO> findList(PrepareCoursewarePartQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区互动课件切片数据
     */
    @Override
    public PrepareCoursewarePartEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区互动课件切片数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<PrepareCoursewarePartEntity> findByCoursewareId(Long coursewareId) {
        LambdaQueryWrapper<PrepareCoursewarePartEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareCoursewarePartEntity::getCoursewareId, coursewareId);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByCoursewareId(Long coursewareId) {
        List<PrepareCoursewarePartEntity> partList = findByCoursewareId(coursewareId);
        if (CollectionUtils.isNotEmpty(partList)) {
            deletedByIds(partList.stream().map(PrepareCoursewarePartEntity::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(PrepareCoursewareEntity source, PrepareCoursewareEntity target) {
        List<PrepareCoursewarePartEntity> list = findByCoursewareId(source.getId());
        list.forEach(v -> copy(v, target));
    }

    private void copy(PrepareCoursewarePartEntity source, PrepareCoursewareEntity target) {
        PrepareCoursewarePartEntity entity = prepareCoursewarePartConvert.copy(source, new PrepareCoursewarePartEntity());
        entity.setPrepareId(target.getPrepareId());
        entity.setCoursewareId(target.getId());
        save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void move(PrepareCoursewareEntity entity) {
        LambdaUpdateWrapper<PrepareCoursewarePartEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PrepareCoursewarePartEntity::getCoursewareId, entity.getId());
        wrapper.set(PrepareCoursewarePartEntity::getPrepareId, entity.getPrepareId());
        this.update(wrapper);
    }

}
